<?php

namespace apps\marriage\app\op\controller;

use apps\marriage\core\service\VipCardNoService;
use apps\marriage\core\service\VipCardService;
use think\Request;

class VipCardController extends BaseMarriageOpController
{
    /**
     * @var VipCardService
     */
    private $vipCardService;

    /**
     * @var VipCardNoService
     */
    private $vipCardNoService;

    protected function init()
    {
        parent::init();
        $this->vipCardService = service('VipCard', SERVICE_NAMESPACE);
        $this->vipCardNoService = service('VipCardNo', SERVICE_NAMESPACE);
    }

    public function save(Request $request)
    {
        $data = $request->param();
        $this->vipCardService->save($data);
        return $this->success();
    }

    public function lists(Request $request)
    {
        $pageSize = $request->param('page_size', 10, 'intval');
        $res = $this->vipCardService->pageListByParams([], $pageSize);
        foreach ($res['data'] as &$rs) {
            $rs = $this->vipCardService->format($rs);
        }
        return $this->success($res);
    }

    public function info(Request $request)
    {
        $id = $request->param('id', 10, 'intval');
        $card = $this->vipCardService->getByPk($id);
        $card = $this->vipCardService->format($card);
        return $this->success(['card' => $card]);
    }

    public function all()
    {
        $cards = $this->vipCardService->getAll(['enable' => 1]);
        return $this->success(['cards' => $cards]);
    }

    public function online(Request $request)
    {
        $id = $request->param('id', 10, 'intval');
        $this->vipCardService->updateByPk([
            'id' => $id,
            'enable' => 1
        ]);
        return $this->success();
    }

    public function offline(Request $request)
    {
        $id = $request->param('id', 10, 'intval');
        $this->vipCardService->updateByPk([
            'id' => $id,
            'enable' => 0
        ]);
        return $this->success();
    }
}